package com.wsc.wisdomchinaqinghe.Tools;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Rect;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;

import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.view.SelectPopupWindow;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by cretin on 2017/5/4.
 */

public class OpenExternalMapAppUtils {
    private static String[] paks = new String[]{"com.autonavi.minimap",//高德
            "com.baidu.BaiduMap"};     //百度
    private static SelectPopupWindow menuWindow;

    /**
     * 打开地图显示路径规划 网页版没有路径规划
     *
     * @param activity
     * @param sLongitude
     * @param sLatitude
     * @param sName
     * @param dLongitude
     * @param dLatitude
     * @param dName
     * @param appName
     */
    public static void openMapDirection(Activity activity, String sLongitude, String sLatitude,
                                        String sName, String dLongitude, String dLatitude,
                                        String dName, String appName) {
        List<String> mapApps = getMapApps(activity);
        if ( mapApps != null && !mapApps.isEmpty() ) {
            //有安装客户端 打开PopWindow显示数据
            if ( mapApps.contains(paks[0]) && mapApps.contains(paks[1]) ) {
                showAlertDialog(activity, true, true,
                        sLongitude, sLatitude, sName, dLongitude, dLatitude, dName, appName);
            } else if ( mapApps.contains(paks[0]) ) {
                showAlertDialog(activity, true, false,
                        sLongitude, sLatitude, sName, dLongitude, dLatitude, dName, appName);
            } else if ( mapApps.contains(paks[1]) ) {
                showAlertDialog(activity,false, true,
                        sLongitude, sLatitude, sName, dLongitude, dLatitude, dName, appName);
            }
        } else {
            //没有安装客户端 打开网页版
            Toast.makeText(activity, "请下载百度或高德地图客户端", Toast.LENGTH_SHORT).show();
        }
    }

    //************************************************************************
    //*************************    百度专区     *******************************
    //************************************************************************

    /**
     * 调起百度客户端 路径规划
     * lat,lng (先纬度，后经度)
     * 40.057406655722,116.2964407172
     * lat,lng,lat,lng (先纬度，后经度, 先左下,后右上)
     *
     * @param activity
     */
    public static void openBaiduiDrectionMap(Context activity, String sLongitude, String sLatitude, String sName,
                                             String dLongitude, String dLatitude, String dName) {
        try {
            Intent intent = new Intent("android.intent.action.VIEW",
                    android.net.Uri.parse("baidumap://map/direction?origin=name:" +
                            sName + "|latlng:" + sLatitude + "," + sLongitude + "&destination=" +
                            dName + "&" +
                            "mode=driving&sy=0&index=0&target=0"));
            activity.startActivity(intent);
        }catch (Exception e)
        {

        }

    }


    //************************************************************************
    //*************************    高德专区     *******************************
    //************************************************************************


    /**
     * 调起高德客户端 路径规划
     * lat,lng (先纬度，后经度)
     * 40.057406655722,116.2964407172
     * 输入起点和终点，搜索公交、驾车或步行的线路。支持版本 V4.2.1 起。
     *
     * @param activity
     * @param sLongitude
     * @param sLatitude
     * @param sName
     * @param dLongitude
     * @param dLatitude
     * @param dName
     * @param appName
     */
    public static void openGaodeRouteMap(Context activity, String sLongitude, String sLatitude, String sName,
                                         String dLongitude, String dLatitude, String dName, String appName) {
        try {
            Intent intent = new Intent("android.intent.action.VIEW",
                    android.net.Uri.parse("amapuri://route/plan/?sourceApplication=" + appName +
                            "&sid=&slat=" + sLatitude + "&slon=" +
                            sLongitude + "&sname=" + sName + "&did=&dlat=" +
                            dLatitude + "&dlon=" + dLongitude + "&dname=" + dName + "&dev=1&t=0"));
            intent.setPackage("com.autonavi.minimap");
            activity.startActivity(intent);
        }catch (Exception e)
        {

        }
    }


    /**
     * 显示对话框
     */
    public static void showAlertDialog(final Activity activity, boolean showGaode,
                                       boolean showBaidu, final String longitude,
                                       final String latitude, final String title,
                                       final String dLongitude,
                                       final String dLatitude, final String dName,
                                       final String appName) {
        menuWindow = new SelectPopupWindow(activity, new SelectPopupWindow.OnPopWindowClickListener() {
            @Override
            public void onPopWindowClickListener(View view) {
                if ( view.getId() == R.id.btn_select_baidu ) {
                    //百度
                    openBaiduiDrectionMap(activity, longitude, latitude, title, dLongitude, dLatitude, dName);
                } else if ( view.getId() == R.id.btn_select_gaode ) {
                    //高德
                    openGaodeRouteMap(activity, longitude, latitude, title, dLongitude, dLatitude, dName, appName);
                }
            }
        }, showGaode, showBaidu);
        Rect rect = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        int winHeight = activity.getWindow().getDecorView().getHeight();
        menuWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.BOTTOM, 0, winHeight - rect.bottom);
    }

    /**
     * 通过包名获取应用信息
     *
     * @param context
     * @param packageName
     * @return
     */
    private static String getAppInfoByPak(Context context, String packageName) {
        PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        for ( PackageInfo packageInfo : packageInfos ) {
            if ( packageName.equals(packageInfo.packageName) ) {
                return packageName;
            }
        }
        return null;
    }

    /**
     * 返回当前设备上的地图应用集合
     *
     * @param context
     * @return
     */
    private static List<String> getMapApps(Context context) {
        LinkedList<String> apps = new LinkedList<>();
        for ( String pak : paks ) {
            String appinfo = getAppInfoByPak(context, pak);
            if ( appinfo != null ) {
                apps.add(appinfo);
            }
        }
        return apps;
    }
}
